package 力扣;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author yyq
 * @create 2022-04-11 9:59
 */
public class leetcode257 {
    public static void main(String[] args) {
        Stack<Integer> stack=new Stack<>();
        stack.push(1);
        stack.push(2);
        for (int i = 0; i < stack.size(); i++) {
            String s="1"+String.valueOf(stack.get(i));
            System.out.println(stack.get(i));
        }
    }
    public List<String> binaryTreePaths(TreeNode root) {
        if(root==null) return null;
        List<String> list=new ArrayList<>();
        Stack<Integer> stack=new Stack<>();
        preOrder(list,stack,root);
        return list;
    }

    private void preOrder(List<String> list, Stack<Integer> stack, TreeNode root) {
        stack.push(root.val);
        // 发现叶子节点 list添加一条路径
        if(root.left==null&&root.right==null){
            String s="";
            for (int i = 0; i < stack.size(); i++) {
                if(i!=stack.size()-1)
                    s = s + stack.get(i) +"->";
                if(i==stack.size()-1)
                    s = s + stack.get(i);
            }
            list.add(s);
        }
        if(root.left!=null) preOrder(list,stack,root.left);
        if(root.right!=null) preOrder(list,stack,root.right);
        stack.pop();
    }


}
